{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "navegator = webdriver.Firefox()\n",
    "#navegator = webdriver.ChromeService(executable_path=r'C:\\Users\\Arthur\\anaconda3\\chromedriver')\n",
    "navegator.get('https://www3.bcb.gov.br/ifdata/#!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "from selenium.webdriver.common.by import By\n",
    "time.sleep(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium.webdriver.support.ui import Select"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "time.sleep(5)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#/html/body/form/div[2]/div[2]/div/div[1]/ul/li[73]/a\n",
    "#/html/body/form/div[2]/div[2]/div/div[1]/ul/li[2]/a\n",
    "\n",
    "#/html/body/form/div[2]/div[2]/div/div[1]/ul/li[24]/a\n",
    "#/html/body/form/div[2]/div[2]/div/div[1]/ul/li[23]/a\n",
    "#/html/body/form/div[2]/div[2]/div/div[1]/ul/li[24]/a\n",
    "\n",
    "\n",
    "#/html/body/form/div[2]/div[2]/div/div[1]/button[2]\n",
    "#//*[@id=\"btnDataBase\"]\n",
    "\n",
    "   \n",
    "    #Tipo de instituição:\n",
    "\n",
    "#/html/body/form/div[2]/div[2]/div/div[2]/button[2]\n",
    "#//*[@id=\"btnTipoInst\"]\n",
    "#//*[@id=\"btnTipoInst\"]\n",
    "#/html/body/form/div[2]/div[2]/div/div[2]/button[2]\n",
    "\n",
    "#/html/body/form/div[2]/div[2]/div/div[2]/ul/li[3]/a\n",
    "\n",
    "    #Tipo de relatório: Resumo\n",
    "\n",
    "#/html/body/form/div[2]/div[2]/div/div[3]/button[2]\n",
    "#//*[@id=\"btnRelatorio\"]\n",
    "\n",
    "#/html/body/form/div[2]/div[2]/div/div[3]/ul/li[1]/a\n",
    "\n",
    "\n",
    "#navegator.execute_script(\"window.scrollTo(0, Y)\")\n",
    "\n",
    "#iframe = navegator.find_element(By.XPATH, \"html/body/form/div[2]/div[2]/div/div[1]/ul/li[30]/a\")\n",
    "   # ActionChains(navegator)\\\n",
    "      #  .scroll_to_element(iframe)\\\n",
    "     #   .perform()  \n",
    "#navegator.scroll_to_ele        \n",
    "#navegator.spec_from_file_location(name, location, *, loader=None, submodule_search_locations=None)\n",
    "\n",
    "#navegator.find_element(By.XPATH, '//*[@id=\"btnDataBase\"]').click()\n",
    "#time.sleep(1)\n",
    "#navegator.execute_script(\"window.scrollTo(0, document.body.scrollHeight);\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#key = 1\n",
    "#count = 0\n",
    "#while key < 23:\n",
    " #   key_add = \"html/body/form/div[2]/div[2]/div/div[1]/ul/li[\"+str(key)+\"]/a\"\n",
    " #   print(key_add)\n",
    "  #  key += 1\n",
    "  #  count += 1\n",
    "  #  print(count)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[1]/a\n",
      "1\n"
     ]
    },
    {
     "ename": "NoSuchElementException",
     "evalue": "Message: Unable to locate element: /html/body/form/div[2]/div[2]/div/div[1]/ul/li[2]/a; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception\nStacktrace:\nRemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8\nWebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:189:5\nNoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:507:5\ndom.find/</<@chrome://remote/content/shared/DOM.sys.mjs:132:16\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNoSuchElementException\u001b[0m                    Traceback (most recent call last)",
      "\u001b[1;32mf:\\BACKUP GERAL 01.06.2020\\UFSC\\2023\\Artigo Earnings Persistence\\3. Selenium coleta com looping - Passivo.ipynb Cell 8\u001b[0m line \u001b[0;36m1\n\u001b[0;32m     <a href='vscode-notebook-cell:/f%3A/BACKUP%20GERAL%2001.06.2020/UFSC/2023/Artigo%20Earnings%20Persistence/3.%20Selenium%20coleta%20com%20looping%20-%20Passivo.ipynb#X10sZmlsZQ%3D%3D?line=12'>13</a>\u001b[0m navegator\u001b[39m.\u001b[39mfind_element(By\u001b[39m.\u001b[39mXPATH, \u001b[39m'\u001b[39m\u001b[39m//*[@id=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mbtnDataBase\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m]\u001b[39m\u001b[39m'\u001b[39m)\u001b[39m.\u001b[39mclick()\n\u001b[0;32m     <a href='vscode-notebook-cell:/f%3A/BACKUP%20GERAL%2001.06.2020/UFSC/2023/Artigo%20Earnings%20Persistence/3.%20Selenium%20coleta%20com%20looping%20-%20Passivo.ipynb#X10sZmlsZQ%3D%3D?line=13'>14</a>\u001b[0m time\u001b[39m.\u001b[39msleep(\u001b[39m2\u001b[39m)\n\u001b[1;32m---> <a href='vscode-notebook-cell:/f%3A/BACKUP%20GERAL%2001.06.2020/UFSC/2023/Artigo%20Earnings%20Persistence/3.%20Selenium%20coleta%20com%20looping%20-%20Passivo.ipynb#X10sZmlsZQ%3D%3D?line=14'>15</a>\u001b[0m navegator\u001b[39m.\u001b[39;49mfind_element(By\u001b[39m.\u001b[39;49mXPATH, \u001b[39m'\u001b[39;49m\u001b[39m/html/body/form/div[2]/div[2]/div/div[1]/ul/li[\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m+\u001b[39;49m\u001b[39mstr\u001b[39;49m(key)\u001b[39m+\u001b[39;49m\u001b[39m'\u001b[39;49m\u001b[39m]/a\u001b[39;49m\u001b[39m'\u001b[39;49m)\u001b[39m.\u001b[39mclick()\n\u001b[0;32m     <a href='vscode-notebook-cell:/f%3A/BACKUP%20GERAL%2001.06.2020/UFSC/2023/Artigo%20Earnings%20Persistence/3.%20Selenium%20coleta%20com%20looping%20-%20Passivo.ipynb#X10sZmlsZQ%3D%3D?line=15'>16</a>\u001b[0m time\u001b[39m.\u001b[39msleep(\u001b[39m2\u001b[39m)\n\u001b[0;32m     <a href='vscode-notebook-cell:/f%3A/BACKUP%20GERAL%2001.06.2020/UFSC/2023/Artigo%20Earnings%20Persistence/3.%20Selenium%20coleta%20com%20looping%20-%20Passivo.ipynb#X10sZmlsZQ%3D%3D?line=16'>17</a>\u001b[0m \u001b[39m#Tipo de instituição:\u001b[39;00m\n",
      "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\selenium\\webdriver\\remote\\webdriver.py:738\u001b[0m, in \u001b[0;36mWebDriver.find_element\u001b[1;34m(self, by, value)\u001b[0m\n\u001b[0;32m    735\u001b[0m     by \u001b[39m=\u001b[39m By\u001b[39m.\u001b[39mCSS_SELECTOR\n\u001b[0;32m    736\u001b[0m     value \u001b[39m=\u001b[39m \u001b[39mf\u001b[39m\u001b[39m'\u001b[39m\u001b[39m[name=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39m{\u001b[39;00mvalue\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m\u001b[39m]\u001b[39m\u001b[39m'\u001b[39m\n\u001b[1;32m--> 738\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mexecute(Command\u001b[39m.\u001b[39;49mFIND_ELEMENT, {\u001b[39m\"\u001b[39;49m\u001b[39musing\u001b[39;49m\u001b[39m\"\u001b[39;49m: by, \u001b[39m\"\u001b[39;49m\u001b[39mvalue\u001b[39;49m\u001b[39m\"\u001b[39;49m: value})[\u001b[39m\"\u001b[39m\u001b[39mvalue\u001b[39m\u001b[39m\"\u001b[39m]\n",
      "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\selenium\\webdriver\\remote\\webdriver.py:344\u001b[0m, in \u001b[0;36mWebDriver.execute\u001b[1;34m(self, driver_command, params)\u001b[0m\n\u001b[0;32m    342\u001b[0m response \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mcommand_executor\u001b[39m.\u001b[39mexecute(driver_command, params)\n\u001b[0;32m    343\u001b[0m \u001b[39mif\u001b[39;00m response:\n\u001b[1;32m--> 344\u001b[0m     \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49merror_handler\u001b[39m.\u001b[39;49mcheck_response(response)\n\u001b[0;32m    345\u001b[0m     response[\u001b[39m\"\u001b[39m\u001b[39mvalue\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39m_unwrap_value(response\u001b[39m.\u001b[39mget(\u001b[39m\"\u001b[39m\u001b[39mvalue\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39mNone\u001b[39;00m))\n\u001b[0;32m    346\u001b[0m     \u001b[39mreturn\u001b[39;00m response\n",
      "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\site-packages\\selenium\\webdriver\\remote\\errorhandler.py:229\u001b[0m, in \u001b[0;36mErrorHandler.check_response\u001b[1;34m(self, response)\u001b[0m\n\u001b[0;32m    227\u001b[0m         alert_text \u001b[39m=\u001b[39m value[\u001b[39m\"\u001b[39m\u001b[39malert\u001b[39m\u001b[39m\"\u001b[39m]\u001b[39m.\u001b[39mget(\u001b[39m\"\u001b[39m\u001b[39mtext\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m    228\u001b[0m     \u001b[39mraise\u001b[39;00m exception_class(message, screen, stacktrace, alert_text)  \u001b[39m# type: ignore[call-arg]  # mypy is not smart enough here\u001b[39;00m\n\u001b[1;32m--> 229\u001b[0m \u001b[39mraise\u001b[39;00m exception_class(message, screen, stacktrace)\n",
      "\u001b[1;31mNoSuchElementException\u001b[0m: Message: Unable to locate element: /html/body/form/div[2]/div[2]/div/div[1]/ul/li[2]/a; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#no-such-element-exception\nStacktrace:\nRemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8\nWebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:189:5\nNoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:507:5\ndom.find/</<@chrome://remote/content/shared/DOM.sys.mjs:132:16\n"
     ]
    }
   ],
   "source": [
    "# de dez/2022 a set/2017, pois aqui não precisa dar scroll para baixo\n",
    "key = 1\n",
    "count = 0\n",
    "while key < 23:\n",
    "    key_add = \"html/body/form/div[2]/div[2]/div/div[1]/ul/li[\"+str(key)+\"]/a\"\n",
    "    print(key_add)\n",
    "    key += 1\n",
    "    count += 1\n",
    "    print(count)\n",
    "    \n",
    "    \n",
    "    #Data-base:\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"btnDataBase\"]').click()\n",
    "    time.sleep(2)\n",
    "    navegator.find_element(By.XPATH, '/html/body/form/div[2]/div[2]/div/div[1]/ul/li['+str(key)+']/a').click()\n",
    "    time.sleep(2)\n",
    "    #Tipo de instituição:\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"btnTipoInst\"]').click()\n",
    "    time.sleep(2)\n",
    "    navegator.find_element(By.XPATH, '/html/body/form/div[2]/div[2]/div/div[2]/ul/li[3]/a').click()\n",
    "    #Tipo de relatório: Passivo\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"btnRelatorio\"]').click()\n",
    "    time.sleep(2)\n",
    "    navegator.find_element(By.XPATH, '/html/body/form/div[2]/div[2]/div/div[3]/ul/li[3]/a').click()\n",
    "    time.sleep(20)\n",
    "    #Donwload\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"aExportCsv\"]').click()\n",
    "    time.sleep(6)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#key = 23\n",
    "#count = 0\n",
    "#while key < 37:\n",
    "  #  key_add = \"html/body/form/div[2]/div[2]/div/div[1]/ul/li[\"+str(key)+\"]/a\"\n",
    "  #  print(key_add)\n",
    "  #  key += 1\n",
    "  #  count += 1\n",
    "  #  print(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[23]/a\n",
      "1\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[24]/a\n",
      "2\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[25]/a\n",
      "3\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[26]/a\n",
      "4\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[27]/a\n",
      "5\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[28]/a\n",
      "6\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[29]/a\n",
      "7\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[30]/a\n",
      "8\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[31]/a\n",
      "9\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[32]/a\n",
      "10\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[33]/a\n",
      "11\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[34]/a\n",
      "12\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[35]/a\n",
      "13\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[36]/a\n",
      "14\n"
     ]
    }
   ],
   "source": [
    "# de jun/2017 até mar/2014, aqui precisa dar scroll para baixo\n",
    "key = 23\n",
    "count = 0\n",
    "while key < 37:\n",
    "    key_add = \"html/body/form/div[2]/div[2]/div/div[1]/ul/li[\"+str(key)+\"]/a\"\n",
    "    print(key_add)\n",
    "    key += 1\n",
    "    count += 1\n",
    "    print(count)\n",
    "    \n",
    "    \n",
    "    #Data-base:\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"btnDataBase\"]').click()\n",
    "    time.sleep(2)\n",
    "    # Execute um scroll down\n",
    "    navegator.execute_script(\"window.scrollTo(0, document.body.scrollHeight);\")  \n",
    "    time.sleep(3)\n",
    "    navegator.find_element(By.XPATH, '/html/body/form/div[2]/div[2]/div/div[1]/ul/li['+str(key)+']/a').click()\n",
    "    time.sleep(3)\n",
    "    # Execute um scroll up\n",
    "    navegator.execute_script(\"window.scrollTo(0, -document.body.scrollHeight);\")\n",
    "    #Tipo de instituição:\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"btnTipoInst\"]').click()\n",
    "    time.sleep(2)\n",
    "    navegator.find_element(By.XPATH, '/html/body/form/div[2]/div[2]/div/div[2]/ul/li[3]/a').click()\n",
    "    time.sleep(2)\n",
    "    #Tipo de relatório: Passivo\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"btnRelatorio\"]').click()\n",
    "    time.sleep(2)\n",
    "    navegator.find_element(By.XPATH, '/html/body/form/div[2]/div[2]/div/div[3]/ul/li[3]/a').click()\n",
    "    time.sleep(22)\n",
    "    #Donwload\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"aExportCsv\"]').click()\n",
    "    time.sleep(6)\n",
    "\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    # Não adianta pq o csv sempre vem com tudo\n",
    "    #Selecionar apenas cooperativas\n",
    "    #navegator.find_element(By.XPATH, '/html/body/form/div[7]/table[1]/thead/tr/th[5]/div/a[2]/i').click()\n",
    "   # time.sleep(1)\n",
    "   # navegator.find_element(By.XPATH, '//*[@id=\"cbFiltroAll\"]').click()\n",
    "   # time.sleep(1)\n",
    "  #  navegator.find_element(By.XPATH, '//*[@id=\"cbFiltro8\"]').click()\n",
    "  #  time.sleep(1)\n",
    "   # navegator.find_element(By.XPATH, '//*[@id=\"btnOk\"]').click()\n",
    "  #  time.sleep(4)\n",
    "   # navegator.find_element(By.XPATH, '//*[@id=\"aExportCsv\"]').click()\n",
    "  #  time.sleep(4)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "#09/2022###/html/body/form/div[2]/div[2]/div/div[1]/ul/li[3]/a\n",
    "\n",
    "#03/2010#final#/html/body/form/div[2]/div[2]/div/div[1]/ul/li[53]/a\n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "    \n",
    "#if range(print(key_add)) == 50:\n",
    "    #print(\"Job Done!\")    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#key = 37\n",
    "#count = 0\n",
    "#while key < 74:\n",
    "   # key_add = \"html/body/form/div[2]/div[2]/div/div[1]/ul/li[\"+str(key)+\"]/a\"\n",
    "   # print(key_add)\n",
    "   # key += 1\n",
    "   # count += 1\n",
    "   # print(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[37]/a\n",
      "1\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[38]/a\n",
      "2\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[39]/a\n",
      "3\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[40]/a\n",
      "4\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[41]/a\n",
      "5\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[42]/a\n",
      "6\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[43]/a\n",
      "7\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[44]/a\n",
      "8\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[45]/a\n",
      "9\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[46]/a\n",
      "10\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[47]/a\n",
      "11\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[48]/a\n",
      "12\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[49]/a\n",
      "13\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[50]/a\n",
      "14\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[51]/a\n",
      "15\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[52]/a\n",
      "16\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[53]/a\n",
      "17\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[54]/a\n",
      "18\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[55]/a\n",
      "19\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[56]/a\n",
      "20\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[57]/a\n",
      "21\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[58]/a\n",
      "22\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[59]/a\n",
      "23\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[60]/a\n",
      "24\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[61]/a\n",
      "25\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[62]/a\n",
      "26\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[63]/a\n",
      "27\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[64]/a\n",
      "28\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[65]/a\n",
      "29\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[66]/a\n",
      "30\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[67]/a\n",
      "31\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[68]/a\n",
      "32\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[69]/a\n",
      "33\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[70]/a\n",
      "34\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[71]/a\n",
      "35\n",
      "html/body/form/div[2]/div[2]/div/div[1]/ul/li[72]/a\n",
      "36\n"
     ]
    }
   ],
   "source": [
    "# de dez/2013 a mar/2005, aqui precisa dar scroll para baixo e mudou instituições individuais está na segunda posição\n",
    "key = 37\n",
    "count = 0\n",
    "while key < 73:\n",
    "    key_add = \"html/body/form/div[2]/div[2]/div/div[1]/ul/li[\"+str(key)+\"]/a\"\n",
    "    print(key_add)\n",
    "    key += 1\n",
    "    count += 1\n",
    "    print(count)\n",
    "    \n",
    "    \n",
    "    #Data-base:\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"btnDataBase\"]').click()\n",
    "    time.sleep(2)\n",
    "    # Execute um scroll down\n",
    "    navegator.execute_script(\"window.scrollTo(0, document.body.scrollHeight);\")  \n",
    "    time.sleep(3)\n",
    "    navegator.find_element(By.XPATH, '/html/body/form/div[2]/div[2]/div/div[1]/ul/li['+str(key)+']/a').click()\n",
    "    time.sleep(3)\n",
    "    # Execute um scroll up\n",
    "    navegator.execute_script(\"window.scrollTo(0, -document.body.scrollHeight);\")\n",
    "    #Tipo de instituição:\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"btnTipoInst\"]').click()\n",
    "    time.sleep(2)\n",
    "    #Aqui mudou pois agora instituições individuais está na segunda posição e não na terceira\n",
    "    navegator.find_element(By.XPATH, '/html/body/form/div[2]/div[2]/div/div[2]/ul/li[2]/a').click()\n",
    "    time.sleep(2)\n",
    "    #Tipo de relatório: Passivo\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"btnRelatorio\"]').click()\n",
    "    time.sleep(2)\n",
    "    navegator.find_element(By.XPATH, '/html/body/form/div[2]/div[2]/div/div[3]/ul/li[3]/a').click()\n",
    "    time.sleep(22)\n",
    "    #Donwload\n",
    "    navegator.find_element(By.XPATH, '//*[@id=\"aExportCsv\"]').click()\n",
    "    time.sleep(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Crie uma lista com os nomes dos arquivos gerados\n",
    "#nomes_gerados = []\n",
    "#for number in range(72):\n",
    "#    if number == 0:\n",
    "  #      nomes_gerados.append(\"dados\")\n",
    " #   else:\n",
    "  #      nomes_gerados.append(f\"dados({number})\")\n",
    "        \n",
    "#print(nomes_gerados)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Crie uma lista de datas trimestrais no formato \"mm-yyyy\"\n",
    "#datas_trimestrais = [\"12-2022\", \"09-2022\", \"06-2022\", \"03-2022\", \"12-2021\", \"09-2021\", \"06-2021\", \"03-2021\", \"12-2020\", \"09-2020\", \"06-2020\", \"03-2020\", \"12-2019\", \"09-2019\", \"06-2019\", \"03-2019\", \"12-2018\", \"09-2018\", \"06-2018\", \"03-2018\", \"12-2017\", \"09-2017\", \"06-2017\", \"03-2017\", \"12-2016\", \"09-2016\", \"06-2016\", \"03-2016\", \"12-2015\", \"09-2015\", \"06-2015\", \"03-2015\", \"12-2014\", \"09-2014\", \"06-2014\", \"03-2014\", \"12-2013\", \"09-2013\", \"06-2013\", \"03-2013\", \"12-2012\", \"09-2012\", \"06-2012\", \"03-2012\", \"12-2011\", \"09-2011\", \"06-2011\", \"03-2011\", \"12-2010\", \"09-2010\", \"06-2010\", \"03-2010\", \"12-2009\", \"09-2009\", \"06-2009\", \"03-2009\", \"12-2008\", \"09-2008\", \"06-2008\", \"03-2008\", \"12-2007\", \"09-2007\", \"06-2007\", \"03-2007\", \"12-2006\", \"09-2006\", \"06-2006\", \"03-2006\", \"12-2005\", \"09-2005\", \"06-2005\", \"03-2005\"]\n",
    "#len(datas_trimestrais)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Arquivo renomeado e movido: dados.csv -> 12-2022.csv\n",
      "Arquivo renomeado e movido: dados(1).csv -> 09-2022.csv\n",
      "Arquivo renomeado e movido: dados(2).csv -> 06-2022.csv\n",
      "Arquivo renomeado e movido: dados(3).csv -> 03-2022.csv\n",
      "Arquivo renomeado e movido: dados(4).csv -> 12-2021.csv\n",
      "Arquivo renomeado e movido: dados(5).csv -> 09-2021.csv\n",
      "Arquivo renomeado e movido: dados(6).csv -> 06-2021.csv\n",
      "Arquivo renomeado e movido: dados(7).csv -> 03-2021.csv\n",
      "Arquivo renomeado e movido: dados(8).csv -> 12-2020.csv\n",
      "Arquivo renomeado e movido: dados(9).csv -> 09-2020.csv\n",
      "Arquivo renomeado e movido: dados(10).csv -> 06-2020.csv\n",
      "Arquivo renomeado e movido: dados(11).csv -> 03-2020.csv\n",
      "Arquivo renomeado e movido: dados(12).csv -> 12-2019.csv\n",
      "Arquivo renomeado e movido: dados(13).csv -> 09-2019.csv\n",
      "Arquivo renomeado e movido: dados(14).csv -> 06-2019.csv\n",
      "Arquivo renomeado e movido: dados(15).csv -> 03-2019.csv\n",
      "Arquivo renomeado e movido: dados(16).csv -> 12-2018.csv\n",
      "Arquivo renomeado e movido: dados(17).csv -> 09-2018.csv\n",
      "Arquivo renomeado e movido: dados(18).csv -> 06-2018.csv\n",
      "Arquivo renomeado e movido: dados(19).csv -> 03-2018.csv\n",
      "Arquivo renomeado e movido: dados(20).csv -> 12-2017.csv\n",
      "Arquivo renomeado e movido: dados(21).csv -> 09-2017.csv\n",
      "Arquivo renomeado e movido: dados(22).csv -> 06-2017.csv\n",
      "Arquivo renomeado e movido: dados(23).csv -> 03-2017.csv\n",
      "Arquivo renomeado e movido: dados(24).csv -> 12-2016.csv\n",
      "Arquivo renomeado e movido: dados(25).csv -> 09-2016.csv\n",
      "Arquivo renomeado e movido: dados(26).csv -> 06-2016.csv\n",
      "Arquivo renomeado e movido: dados(27).csv -> 03-2016.csv\n",
      "Arquivo renomeado e movido: dados(28).csv -> 12-2015.csv\n",
      "Arquivo renomeado e movido: dados(29).csv -> 09-2015.csv\n",
      "Arquivo renomeado e movido: dados(30).csv -> 06-2015.csv\n",
      "Arquivo renomeado e movido: dados(31).csv -> 03-2015.csv\n",
      "Arquivo renomeado e movido: dados(32).csv -> 12-2014.csv\n",
      "Arquivo renomeado e movido: dados(33).csv -> 09-2014.csv\n",
      "Arquivo renomeado e movido: dados(34).csv -> 06-2014.csv\n",
      "Arquivo renomeado e movido: dados(35).csv -> 03-2014.csv\n",
      "Arquivo renomeado e movido: dados(36).csv -> 12-2013.csv\n",
      "Arquivo renomeado e movido: dados(37).csv -> 09-2013.csv\n",
      "Arquivo renomeado e movido: dados(38).csv -> 06-2013.csv\n",
      "Arquivo renomeado e movido: dados(39).csv -> 03-2013.csv\n",
      "Arquivo renomeado e movido: dados(40).csv -> 12-2012.csv\n",
      "Arquivo renomeado e movido: dados(41).csv -> 09-2012.csv\n",
      "Arquivo renomeado e movido: dados(42).csv -> 06-2012.csv\n",
      "Arquivo renomeado e movido: dados(43).csv -> 03-2012.csv\n",
      "Arquivo renomeado e movido: dados(44).csv -> 12-2011.csv\n",
      "Arquivo renomeado e movido: dados(45).csv -> 09-2011.csv\n",
      "Arquivo renomeado e movido: dados(46).csv -> 06-2011.csv\n",
      "Arquivo renomeado e movido: dados(47).csv -> 03-2011.csv\n",
      "Arquivo renomeado e movido: dados(48).csv -> 12-2010.csv\n",
      "Arquivo renomeado e movido: dados(49).csv -> 09-2010.csv\n",
      "Arquivo renomeado e movido: dados(50).csv -> 06-2010.csv\n",
      "Arquivo renomeado e movido: dados(51).csv -> 03-2010.csv\n",
      "Arquivo renomeado e movido: dados(52).csv -> 12-2009.csv\n",
      "Arquivo renomeado e movido: dados(53).csv -> 09-2009.csv\n",
      "Arquivo renomeado e movido: dados(54).csv -> 06-2009.csv\n",
      "Arquivo renomeado e movido: dados(55).csv -> 03-2009.csv\n",
      "Arquivo renomeado e movido: dados(56).csv -> 12-2008.csv\n",
      "Arquivo renomeado e movido: dados(57).csv -> 09-2008.csv\n",
      "Arquivo renomeado e movido: dados(58).csv -> 06-2008.csv\n",
      "Arquivo renomeado e movido: dados(59).csv -> 03-2008.csv\n",
      "Arquivo renomeado e movido: dados(60).csv -> 12-2007.csv\n",
      "Arquivo renomeado e movido: dados(61).csv -> 09-2007.csv\n",
      "Arquivo renomeado e movido: dados(62).csv -> 06-2007.csv\n",
      "Arquivo renomeado e movido: dados(63).csv -> 03-2007.csv\n",
      "Arquivo renomeado e movido: dados(64).csv -> 12-2006.csv\n",
      "Arquivo renomeado e movido: dados(65).csv -> 09-2006.csv\n",
      "Arquivo renomeado e movido: dados(66).csv -> 06-2006.csv\n",
      "Arquivo renomeado e movido: dados(67).csv -> 03-2006.csv\n",
      "Arquivo renomeado e movido: dados(68).csv -> 12-2005.csv\n",
      "Arquivo renomeado e movido: dados(69).csv -> 09-2005.csv\n",
      "Arquivo renomeado e movido: dados(70).csv -> 06-2005.csv\n",
      "Arquivo renomeado e movido: dados(71).csv -> 03-2005.csv\n",
      "Processo concluído.\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import shutil\n",
    "\n",
    "# Lista de nomes de arquivos gerados\n",
    "nomes_gerados = []\n",
    "for number in range(72):\n",
    "    if number == 0:\n",
    "        nomes_gerados.append(\"dados\")\n",
    "    else:\n",
    "        nomes_gerados.append(f\"dados({number})\")\n",
    "\n",
    "# Lista de datas trimestrais no formato \"mm-yyyy\"\n",
    "datas_trimestrais = [\"12-2022\", \"09-2022\", \"06-2022\", \"03-2022\", \"12-2021\", \"09-2021\", \"06-2021\", \"03-2021\", \"12-2020\", \"09-2020\", \"06-2020\", \"03-2020\", \"12-2019\", \"09-2019\", \"06-2019\", \"03-2019\", \"12-2018\", \"09-2018\", \"06-2018\", \"03-2018\", \"12-2017\", \"09-2017\", \"06-2017\", \"03-2017\", \"12-2016\", \"09-2016\", \"06-2016\", \"03-2016\", \"12-2015\", \"09-2015\", \"06-2015\", \"03-2015\", \"12-2014\", \"09-2014\", \"06-2014\", \"03-2014\", \"12-2013\", \"09-2013\", \"06-2013\", \"03-2013\", \"12-2012\", \"09-2012\", \"06-2012\", \"03-2012\", \"12-2011\", \"09-2011\", \"06-2011\", \"03-2011\", \"12-2010\", \"09-2010\", \"06-2010\", \"03-2010\", \"12-2009\", \"09-2009\", \"06-2009\", \"03-2009\", \"12-2008\", \"09-2008\", \"06-2008\", \"03-2008\", \"12-2007\", \"09-2007\", \"06-2007\", \"03-2007\", \"12-2006\", \"09-2006\", \"06-2006\", \"03-2006\", \"12-2005\", \"09-2005\", \"06-2005\", \"03-2005\"]\n",
    "\n",
    "# Pasta de origem dos arquivos CSV\n",
    "pasta_origem = \"C:\\\\Users\\\\Arthur\\\\Downloads\"\n",
    "\n",
    "# Pasta de destino para os arquivos renomeados\n",
    "pasta_destino = \"F:\\\\BACKUP GERAL 01.06.2020\\\\UFSC\\\\2023\\\\Artigo Earnings Persistence\\\\Dados if.data - dez-2022 - mar-2005 (Passivo)\"\n",
    "\n",
    "# Loop para renomear e mover os arquivos\n",
    "for i in range(len(nomes_gerados)):\n",
    "    nome_original = f\"{nomes_gerados[i]}.csv\"\n",
    "    nome_novo = f\"{datas_trimestrais[i]}.csv\"\n",
    "\n",
    "    caminho_original = os.path.join(pasta_origem, nome_original)\n",
    "    caminho_destino = os.path.join(pasta_destino, nome_novo)\n",
    "\n",
    "    # Verifique se o arquivo original existe\n",
    "    if os.path.exists(caminho_original):\n",
    "        # Renomeie e mova o arquivo para a pasta de destino\n",
    "        shutil.move(caminho_original, caminho_destino)\n",
    "        print(f\"Arquivo renomeado e movido: {nome_original} -> {nome_novo}\")\n",
    "    else:\n",
    "        print(f\"Arquivo original não encontrado: {nome_original}\")\n",
    "\n",
    "print(\"Processo concluído.\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
